package com.guacun.dao;


import com.guacun.model.Teacher;

public class TeacherDao {
    public static Teacher[] teacherinfo = new Teacher[100];
    static {
        for (int i = 0; i < 10; i++) {
            teacherinfo[i]=new Teacher(i+1,"老师"+(i+1),11+i,"2000-"+2+"-"+(i+i));
        }

    }
    public int add(Teacher[] list) {
        int flag = 0;
        for (int i = 0; i < teacherinfo.length; i++) {
            if (null == teacherinfo[i]) {
                teacherinfo[i] = list[0];
                flag = 1;
                System.out.println("添加老师成功！");
                break;
            } else {
                System.out.println("添加老师失败！");
            }
        }

        return flag;
    }

    public int selectall() {
        for (int i = 0; i < teacherinfo.length; i++) {
            if (null != teacherinfo[i]) {
                System.out.println(teacherinfo[i]);
            }
        }
        int i = 1;
        return i;
    }

    public int deleteteacher(int id) {
        int i = 0;
        for (int j = 0; j < teacherinfo.length; j++) {
            if (teacherinfo[j].getId() == id) {
                for (int k = j; k < teacherinfo.length - 1; k++) {
                    teacherinfo[k] = teacherinfo[k + 1];
                }
                i = 1;
                break;
            }
        }
        return i;
    }

    public Teacher selectallbyid(int id) {
        Teacher teacher = null;

        for (int i = 0; i < teacherinfo.length; i++) {
            try {
                if (teacherinfo[i].getId() == id) {
                    teacher = teacherinfo[i];
                }
            } catch (NullPointerException e) {
                //  System.out.println("捕捉到的异常：" + e.getMessage());
            }
        }


        return teacher;
    }

    public int updateteacherdao(Teacher teacher) {
        int i = 0;
        for (int k = 0; k < teacherinfo.length; k++) {
            try {
                if (teacherinfo[k].getId() == teacher.getId()) {
                    teacherinfo[k] = teacher;
                    i = 1;
                }
            } catch (NullPointerException e) {
                //System.out.println("捕捉到的异常：" + e.getMessage());
            }
        }
        return i;
    }
}
